package com.yunfangdata.web.controller;


import com.yunfangdata.domain.Customer;
import com.yunfangdata.query.CustomerQueryObject;
import com.yunfangdata.service.ICustomerService;
import com.yunfangdata.util.JsonResult;
import com.yunfangdata.util.PageResult;
import com.yunfangdata.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @RequiresPermissions("customer:index")
    @PermissionName("客户首页")
    @RequestMapping("")
    public String index(){
        return "customer/list";
    }


    @RequiresPermissions("customer:list")
    @PermissionName("客户列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(CustomerQueryObject qo){
        PageResult query = customerService.query(qo);
        return query;
    }

    @RequiresPermissions("customer:save")
    @PermissionName("客户保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Customer customer){
        JsonResult result = new JsonResult();
        try{
            customerService.save(customer);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult("保存错误");
        }
        return result;
    }




    @RequiresPermissions("customer:changeState")
    @PermissionName("客户状态")
    @RequestMapping("changeState")
    @ResponseBody
    public  JsonResult changeSate(Long id){
        JsonResult result = new JsonResult();
        try {
            customerService.changeState(id);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult("修改错误");
        }
        return result;

    }

    @RequiresPermissions("customer:changeFollow")
    @PermissionName("客户跟进")
    @RequestMapping("changeFollow")
    @ResponseBody
    public  JsonResult changeFollow(Long id){
        JsonResult result = new JsonResult();
        try {
            customerService.changeFollow(id);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult("修改错误");
        }
        return result;

    }


    @RequiresPermissions("customer:update")
    @PermissionName("客户更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Customer entity ){
        JsonResult result = new JsonResult();
        try {
            customerService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult("更新失败");
        }
        return result;
    }



    //查询所有的客户
    @RequestMapping("selectBycus")
    @ResponseBody
    public List<Customer> selectBycus(){
        List<Customer> list = customerService.list();
        return list;
    }




}
